package com.zhangqian.downloadtest;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import static android.R.id.input;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };
    long myDownloadReference;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int permission = ActivityCompat.checkSelfPermission(this,
                "android.permission.WRITE_EXTERNAL_STORAGE");
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 没有写的权限，去申请写的权限，会弹出对话框
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }
        String url = "http://www.170mv.com/kw/other.web.ra01.sycdn.kuwo.cn/resource/n3/128/5/24/3280974577.mp3";


        downloadNauhe(url);
    }

    public void downloadNauhe(final String url2) {

        class DownloadFile extends AsyncTask<String, Integer, String> {
            @Override
            protected String doInBackground(String... url) {
                int count;
                try {
                    URL url1 = new URL(url2);
                    URLConnection conexion = url1.openConnection();
                    conexion.connect();
                    int lenghtOfFile = conexion.getContentLength();
                    InputStream input = new BufferedInputStream(url1.openStream());
                    OutputStream output = new FileOutputStream("/sdcard/01_manum_abbas_as_-_mesum_abbas_2012.mp3");
                    byte data[] = new byte[1024];
                    long total = 0;
                    System.out.println("downloading.............");
                    while ((count = input.read(data)) != -1) {
                        total += count;
                        publishProgress((int)((total/(float)lenghtOfFile)*100));
                        output.write(data, 0, count);
                    }

                    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    scanIntent.setData(Uri.fromFile(new File("/sdcard/01_manum_abbas_as_-_mesum_abbas_2012.mp3")));
                    sendBroadcast(scanIntent);
                    output.flush();
                    output.close();
                    input.close();
                } catch (Exception e) {
                }

                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
//                mprBar.setProgress(values[0]);
            }
        }
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.execute(url2);
    }
}


http://blog.csdn.net/iwanghang/article/details/51448597


        Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);  
        scanIntent.setData(Uri.fromFile(new File(filePath)));  
        context.sendBroadcast(scanIntent);  